import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:rabbit_mobile/view/ShoppingCart/widget/shopping_cart_computed.dart';
import 'package:rabbit_mobile/view/ShoppingCart/widget/shopping_cart_guess.dart';
import 'package:rabbit_mobile/view/ShoppingCart/widget/shopping_cart_module.dart';
import 'package:rabbit_mobile/widget/refresher_widget/index.dart';

import '../../provide/riverpod_provider.dart';
import 'models/shopping_cart_model.dart';

class ShoppingCart extends ConsumerStatefulWidget {
  const ShoppingCart({super.key});

  @override
  _ShoppingCartState createState() => _ShoppingCartState();
}

class _ShoppingCartState extends ConsumerState<ShoppingCart> {
  /// refresher controller
  late RefreshController _refreshController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _refreshController = RefreshController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _refreshController.dispose();
  }

  void _onLoading() async {
    await Future.delayed(const Duration(milliseconds: 1000));
    if (mounted) setState(() {});
    _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    final shoppingCartData = ref.watch(getShoppingProvider);
    final guessYouLike = ref.watch(guessYouLikeProvider);
    ref.listen<AsyncValue<ShoppingCartModel>>(getShoppingProvider, (prev, next) {
      if (next.hasValue && next.value != null) {
        ref.read(shoppingCartIsSelectProvide.notifier).setInitialData(next.value!.valids);
      }
    });

    return Scaffold(
      appBar: const ShoppingCartAppBar(),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 12, bottom: 60.0, right: 12, left: 12),
            child: RefresherWidget(
              refreshController: _refreshController,
              enablePullDown: true,
              enablePullUp: false,
              onLoading: _onLoading,
              onRefresh: () {
                ref.refresh(getShoppingProvider.notifier);
                _refreshController.refreshCompleted();
              },
              child: CustomScrollView(
                slivers: [
                  SliverToBoxAdapter(child: ShoppingCartModule(shoppingCartData: shoppingCartData)),
                  SliverToBoxAdapter(child: ShoppingCartGuess(guessYouLike: guessYouLike)),
                  const SliverToBoxAdapter(child: SizedBox(height: 10)),
                ],
              ),
            ),
          ),
          const Positioned(
            right: 0,
            left: 0,
            bottom: 0,
            child: ShoppingCartComputed(),
          ),
        ],
      ),
    );
  }
}

class ShoppingCartAppBar extends StatelessWidget implements PreferredSizeWidget {
  const ShoppingCartAppBar({super.key});

  @override
  // TODO: implement preferredSize
  Size get preferredSize => const Size(double.infinity, kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.width + kToolbarHeight,
      decoration: const BoxDecoration(
        color: Colors.white,
        image: DecorationImage(
          image: AssetImage("assets/home_appBar_bg.png"),
          fit: BoxFit.cover,
        ),
      ),
      child: const SafeArea(
        child: Center(
          child: Text(
            "購物車",
            style: TextStyle(fontSize: 16.0, color: Colors.white),
          ),
        ),
      ),
    );
  }
}
